package com.example.tellwin.home.act;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.home.bean.QuestionBean;
import com.example.tellwin.home.contract.QuestionDetailContract;
import com.example.tellwin.home.presenter.QuestionDetailPresenter;
import com.example.tellwin.view.ShareDialog;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.droidlover.xrichtext.ImageLoader;
import cn.droidlover.xrichtext.XRichText;

public class QuestionDetailActivity extends CpBaseActivty implements QuestionDetailContract.View {

    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.share_iv)
    ImageView shareIv;
    @BindView(R.id.pic_iv)
    ImageView picIv;
    @BindView(R.id.content_tv)
    TextView contentTv;
    @BindView(R.id.attention_tv)
    TextView attentionTv;
    @BindView(R.id.webview)
    WebView webView;
    @BindView(R.id.rich_text)
    XRichText richText;

    @Inject
    QuestionDetailPresenter mPresenter;

    private QuestionBean questionBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_detail);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);
       /* webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //取消滚动条白边效果
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setDefaultTextEncodingName("UTF-8") ;
        webView.getSettings().setBlockNetworkImage(false);*/
    }

    @Override
    protected void initEvents() {
        attentionTv.setOnClickListener(v -> mPresenter.attention(questionBean.getQuestionId()));
        richText.callback(new XRichText.Callback() {
            @Override
            public void onImageClick(List<String> urlList, int position) {

            }

            @Override
            public boolean onLinkClick(String url) {
                return false;
            }

            @Override
            public void onFix(XRichText.ImageHolder holder) {
                holder.setStyle(XRichText.Style.CENTER);
            }
        });
    }

    @Override
    protected void initDatas() {
        setTitle("文章详情");
        String questionId = getIntent().getStringExtra(Common.QUESTION);
        mPresenter.getQuestionDetail(questionId);
    }

    @OnClick(R.id.share_iv)
    public void onViewClicked() {
        ShareDialog shareDialog = new ShareDialog(this, 2, questionBean.getQuestionId() ,questionBean.getQuestionName());
        shareDialog.show();
    }

    @Override
    public void questionDetailResult(QuestionBean questionBean) {
        this.questionBean = questionBean;
        titleTv.setText(questionBean.getQuestionName());
        contentTv.setText(Html.fromHtml(questionBean.getAnswer()));
        // GlideUtils.LoadImage(this , questionBean.get);
        LogUtils.d(questionBean.getAnswer());
        richText.text(questionBean.getAnswer());

        if (questionBean.getIsFollow() == 0) {
            attentionTv.setText(R.string.attention);
            attentionTv.setSelected(false);
        } else {
            attentionTv.setText(R.string.cancel);
            attentionTv.setSelected(true);
        }
    }

    private String getNewContent(String htmltext) {

        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            if (element.className() != null && element.className().length() > 0)
                element.attr("width", "100%").attr("height", "auto");

        }

        return doc.toString();
    }

    @Override
    public void attention() {
        mPresenter.getQuestionDetail(questionBean.getQuestionId());
    }
}
